import java.awt.Polygon;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;

class Bullet extends Entity
{
	public Bullet(double x, double y, double a, double s)
	{
		this.x = x;
		this.y = y;
		angle = a;
		speed = s;
		
		setToDefaultImage();
		recalculateBoundingBox();
	}
	
	public void setToDefaultImage()
	{
		image = new BufferedImage((int)(2*speed),1,BufferedImage.TYPE_INT_ARGB);
		
		Graphics g = image.getGraphics();
		g.setColor(Color.BLACK);
		g.fillRect(0,0,(int)(2*speed),1);
		
		xloc = new int[]{-2,(int)(2*speed)+2,(int)(2*speed)+2,-2,-2};
		yloc = new int[]{-2,-2,2,2,-2};
	}
	
	public void damage(int d)
	{
		mark();
	}
	
	public double getEndX()
	{
		return x+Math.cos(angle)*speed;
	}
	
	public double getEndY()
	{
		return y+Math.sin(angle)*speed;
	}
	
	public void move()
	{
		x += Math.cos(angle)*speed;
		y += Math.sin(angle)*speed;
	}
	
	public void collide(Entity e)
	{
		if(e instanceof Bullet)
			return;
		
		e.damage(armor);
	}
	
	public void recalculateBoundingBox()
	{
		AffineTransform af = new AffineTransform();
		af.rotate(angle);
		
		int tx[] = new int[xloc.length];
		int ty[] = new int[yloc.length];
		
		for(int a = 0; a < xloc.length; a++)
		{
			Point2D.Double d = (Point2D.Double)af.transform(new Point2D.Double(xloc[a]-image.getWidth(null)/2,yloc[a]-image.getHeight(null)/2),null);
			tx[a] = (int)(d.x)+(int)x;
			ty[a] = (int)(d.y)+(int)y;
		}
		
		boundingBox = new java.awt.Polygon(tx,ty,xloc.length);
	}
}